<?php

namespace App\Http\Resources\Index\Chart;

use App\Http\Resources\Resource;
use App\Core\Traits\Numeral;
use Helper\Timed;

class TransferResource extends Resource
{
    use Numeral;

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $amounts['in'] = array_column($this->resource['in']->toArray(), 'amount', 'timed');
        $amounts['out'] = array_column($this->resource['out']->toArray(), 'amount', 'timed');

        $timedEnd = Timed::getTodayTimed();
        $timed = $timedEnd - 86400 * 99;

        for (; $timed <= $timedEnd; $timed += 86400) {
            $charts['timed'][] = date('Y.m.d', $timed);
            // in
            $charts['data']['in'][] = $in = $amounts['in'][$timed] ?? 0;
            // out
            $charts['data']['out'][] = $out = $amounts['out'][$timed] ?? 0;
            // 差额
            $charts['data']['diff'][] = $this->makeRound($in - $out);
        }

        return $this->getResponse()->setResponseData($charts);
    }
}
